package org.zhongyin.ARXModel;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import jxl.read.biff.BiffException;

import org.zhongyin.Data.NoaaDataWeekend;
import org.zhongyin.Util.LastNHour;

/**
 * 
 * @author ZhongYin Zhang
 *
 */
public class ARXFirstWEProvider extends NoaaDataWeekend implements IARXModel {
	private double[][] ARXmodel_X;
	private double[] ARXmodel_Y;

	public ARXFirstWEProvider() throws ParseException, BiffException,
			IOException {
		super();

	}

	public void setHistdata() throws ParseException {
		super.setHistNoaaData();
		int datasize = super.getHistNoaaData().getDataSize();
		this.ARXmodel_X = new double[datasize][26];
		this.ARXmodel_Y = new double[datasize];
	}

	public void setForecastData() throws ParseException {
		super.setForcastNoaaData();
		int datasize = super.getForcastNoaaData().getDataSize();
		this.ARXmodel_X = new double[datasize][26];
		this.ARXmodel_Y = new double[datasize];
	}

	/**
	 * @return the aRXmodel
	 */
	public double[][] getARXmodel_X() {

		int counter = 0;
		for (int i = 0; i < ARXmodel_X.length; i++) {
			if (ARXmodel_X[i][0] == 0) {
				counter++;
			}
		}

		double[][] result = new double[ARXmodel_X.length - counter][26];

		for (int i = 0, j = 0; i < ARXmodel_X.length && j < result.length; i++) {
			if (ARXmodel_X[i][0] != 0) {
				result[j] = ARXmodel_X[i];
				j++;
			}
		}

		return result;

	}

	/**
	 * @return the aRXmodel_Y
	 */
	public double[] getARXmodel_Y() {
		int counter = 0;
		for (int i = 0; i < ARXmodel_Y.length; i++) {
			if (ARXmodel_Y[i] == 0) {
				counter++;
			}
		}

		double[] result = new double[ARXmodel_X.length - counter];

		for (int i = 0, j = 0; i < ARXmodel_Y.length && j < result.length; i++) {
			if (ARXmodel_Y[i] != 0) {
				result[j] = ARXmodel_Y[i];
				j++;
			}
		}

		return result;
	}

	/**
	 * ARXModel data structure for Historical data
	 * @param aRXmodel
	 *            the aRXmodel to set
	 * @throws ParseException
	 * @throws IOException
	 * @throws BiffException
	 */
	public void setARXHistmodel(int k, int hour) throws ParseException,
			BiffException, IOException {

		Map<Date, String> dateMap = super.getHistNoaaData().getLocalTime();

		List<Date> datelist = new ArrayList<Date>(dateMap.keySet());

		Collections.sort(datelist);

		for (int i = 168; i < datelist.size(); i++) {
			//
			Date date = datelist.get(i);

			// Weekend and Apr to Nov
			if (date.getDay() == 6 || date.getDay() == 0 ) {

				if (date.getHours() == hour) {

					// Constant
					ARXmodel_X[i][0] = 1d;

					// T_Max_t
					ARXmodel_X[i][1] = super.getHistNoaaData().getT_MAX_t()
							.get(date);

					// T_Min_t
					ARXmodel_X[i][2] = super.getHistNoaaData().getT_MIN_t()
							.get(date);

					// T_t
					ARXmodel_X[i][3] = super.getHistNoaaData().getT_t()
							.get(date);

					// T_t * T_t
					ARXmodel_X[i][4] = ARXmodel_X[i][3] * ARXmodel_X[i][3];

					// RH_t
					ARXmodel_X[i][5] = super.getHistNoaaData().getRH_t()
							.get(date);

					// RH_t * T_t
					ARXmodel_X[i][6] = ARXmodel_X[i][5] * ARXmodel_X[i][3];

					// Cool load t-1
					ARXmodel_X[i][7] = LastNHour.getLastNHour(date, k,
							super.getHistNoaaData()).getCoolLoadt();

					// T_t-1
					ARXmodel_X[i][8] = LastNHour.getLastNHour(date, k,
							super.getHistNoaaData()).getTt();

					// T_t-1 * T_t-1
					ARXmodel_X[i][9] = ARXmodel_X[i][8] * ARXmodel_X[i][8];

					// RH_t-1
					ARXmodel_X[i][10] = LastNHour.getLastNHour(date, k,
							super.getHistNoaaData()).getRHt();

					// T_t-1 * RH_t-1
					ARXmodel_X[i][11] = ARXmodel_X[i][8] * ARXmodel_X[i][10];

					// Cool load t-2
					ARXmodel_X[i][12] = LastNHour.getLastNHour(date, k + 1,
							super.getHistNoaaData()).getCoolLoadt();

					// T_t-2
					ARXmodel_X[i][13] = LastNHour.getLastNHour(date, k + 1,
							super.getHistNoaaData()).getTt();

					// T_t-2 * T_t-2
					ARXmodel_X[i][14] = ARXmodel_X[i][13] * ARXmodel_X[i][13];

					// RH_t-2
					ARXmodel_X[i][15] = LastNHour.getLastNHour(date, k + 1,
							super.getHistNoaaData()).getRHt();

					// T_t-2 * RH_t-2
					ARXmodel_X[i][16] = ARXmodel_X[i][13] * ARXmodel_X[i][15];

					// if Saturday, t-24 will be turned into last Sunday
					if (date.getDay() == 6) {
						// Cool load t-24
						ARXmodel_X[i][17] = LastNHour.getLastNHour(date, 144,
								super.getHistNoaaData()).getCoolLoadt();
						// T_t-24
						ARXmodel_X[i][18] = LastNHour.getLastNHour(date, 144,
								super.getHistNoaaData()).getTt();

						// T_t-24 * T_t-24
						ARXmodel_X[i][19] = ARXmodel_X[i][18]
								* ARXmodel_X[i][18];

						// RH_t-24
						ARXmodel_X[i][20] = LastNHour.getLastNHour(date, 144,
								super.getHistNoaaData()).getRHt();

						// T_t-24 * RH_t-24
						ARXmodel_X[i][21] = ARXmodel_X[i][18]
								* ARXmodel_X[i][20];

					} else {
						// Cool load t-24
						ARXmodel_X[i][17] = LastNHour.getLastNHour(date, 24,
								super.getHistNoaaData()).getCoolLoadt();
						// T_t-24
						ARXmodel_X[i][18] = LastNHour.getLastNHour(date, 24,
								super.getHistNoaaData()).getTt();

						// T_t-24 * T_t-24
						ARXmodel_X[i][19] = ARXmodel_X[i][18]
								* ARXmodel_X[i][18];

						// RH_t-24
						ARXmodel_X[i][20] = LastNHour.getLastNHour(date, 24,
								super.getHistNoaaData()).getRHt();

						// T_t-24 * RH_t-24
						ARXmodel_X[i][21] = ARXmodel_X[i][18]
								* ARXmodel_X[i][20];

					}

					// T_t * T_t-1
					ARXmodel_X[i][22] = ARXmodel_X[i][3] * ARXmodel_X[i][8];

					// RH_t * RH_t-1
					ARXmodel_X[i][23] = ARXmodel_X[i][5] * ARXmodel_X[i][8];

					// T_t * T_t-2
					ARXmodel_X[i][24] = ARXmodel_X[i][3] * ARXmodel_X[i][13];

					// RH_t * RH_t-2
					ARXmodel_X[i][25] = ARXmodel_X[i][5] * ARXmodel_X[i][15];

					// Cool load
					ARXmodel_Y[i] = super.getHistNoaaData().getCoolLoad_t()
							.get(date);
				}

			}
		}
	}

	

	/**
	 * ARXModel data structure for Future data
	 * @param k
	 * @throws ParseException
	 * @throws BiffException
	 * @throws IOException
	 */
	public void setARXForcastmodel(int k) throws ParseException, BiffException,
			IOException {

		Map<Date, String> dateMap = super.getForcastNoaaData().getLocalTime();

		List<Date> datelist = new ArrayList<Date>(dateMap.keySet());

		Collections.sort(datelist);

		for (int i = 168; i < datelist.size(); i++) {
			//
			Date date = datelist.get(i);

			if (date.getDay() == 6 || date.getDay() == 0 ) {

				// Constant
				ARXmodel_X[i][0] = 1d;

				// T_Max_t
				ARXmodel_X[i][1] = super.getForcastNoaaData().getT_MAX_t()
						.get(date);

				// T_Min_t
				ARXmodel_X[i][2] = super.getForcastNoaaData().getT_MIN_t()
						.get(date);

				// T_t
				ARXmodel_X[i][3] = super.getForcastNoaaData().getT_t()
						.get(date);

				// T_t * T_t
				ARXmodel_X[i][4] = ARXmodel_X[i][3] * ARXmodel_X[i][3];

				// RH_t
				ARXmodel_X[i][5] = super.getForcastNoaaData().getRH_t()
						.get(date);

				// RH_t * T_t
				ARXmodel_X[i][6] = ARXmodel_X[i][5] * ARXmodel_X[i][3];

				// Cool load t-1
				ARXmodel_X[i][7] = LastNHour.getLastNHour(date, k,
						super.getForcastNoaaData()).getCoolLoadt();

				// T_t-1
				ARXmodel_X[i][8] = LastNHour.getLastNHour(date, k,
						super.getForcastNoaaData()).getTt();

				// T_t-1 * T_t-1
				ARXmodel_X[i][9] = ARXmodel_X[i][8] * ARXmodel_X[i][8];

				// RH_t-1
				ARXmodel_X[i][10] = LastNHour.getLastNHour(date, k,
						super.getForcastNoaaData()).getRHt();

				// T_t-1 * RH_t-1
				ARXmodel_X[i][11] = ARXmodel_X[i][8] * ARXmodel_X[i][10];

				// Cool load t-2
				ARXmodel_X[i][12] = LastNHour.getLastNHour(date, k + 1,
						super.getForcastNoaaData()).getCoolLoadt();

				// T_t-2
				ARXmodel_X[i][13] = LastNHour.getLastNHour(date, k + 1,
						super.getForcastNoaaData()).getTt();

				// T_t-2 * T_t-2
				ARXmodel_X[i][14] = ARXmodel_X[i][13] * ARXmodel_X[i][13];

				// RH_t-2
				ARXmodel_X[i][15] = LastNHour.getLastNHour(date, k + 1,
						super.getForcastNoaaData()).getRHt();

				// T_t-2 * RH_t-2
				ARXmodel_X[i][16] = ARXmodel_X[i][13] * ARXmodel_X[i][15];

				// if Saturday, t-24 will be turned into last Sunday
				if (date.getDay() == 6) {
					// Cool load t-24
					ARXmodel_X[i][17] = LastNHour.getLastNHour(date, 144,
							super.getForcastNoaaData()).getCoolLoadt();
					// T_t-24
					ARXmodel_X[i][18] = LastNHour.getLastNHour(date, 144,
							super.getForcastNoaaData()).getTt();

					// T_t-24 * T_t-24
					ARXmodel_X[i][19] = ARXmodel_X[i][18] * ARXmodel_X[i][18];

					// RH_t-24
					ARXmodel_X[i][20] = LastNHour.getLastNHour(date, 144,
							super.getForcastNoaaData()).getRHt();

					// T_t-24 * RH_t-24
					ARXmodel_X[i][21] = ARXmodel_X[i][18] * ARXmodel_X[i][20];

				} else {
					// Cool load t-24
					ARXmodel_X[i][17] = LastNHour.getLastNHour(date, 24,
							super.getForcastNoaaData()).getCoolLoadt();
					// T_t-24
					ARXmodel_X[i][18] = LastNHour.getLastNHour(date, 24,
							super.getForcastNoaaData()).getTt();

					// T_t-24 * T_t-24
					ARXmodel_X[i][19] = ARXmodel_X[i][18] * ARXmodel_X[i][18];

					// RH_t-24
					ARXmodel_X[i][20] = LastNHour.getLastNHour(date, 24,
							super.getForcastNoaaData()).getRHt();

					// T_t-24 * RH_t-24
					ARXmodel_X[i][21] = ARXmodel_X[i][18] * ARXmodel_X[i][20];

				}

				// T_t * T_t-1
				ARXmodel_X[i][22] = ARXmodel_X[i][3] * ARXmodel_X[i][8];

				// RH_t * RH_t-1
				ARXmodel_X[i][23] = ARXmodel_X[i][5] * ARXmodel_X[i][8];

				// T_t * T_t-2
				ARXmodel_X[i][24] = ARXmodel_X[i][3] * ARXmodel_X[i][13];

				// RH_t * RH_t-2
				ARXmodel_X[i][25] = ARXmodel_X[i][5] * ARXmodel_X[i][15];

				// Cool load
				ARXmodel_Y[i] = super.getForcastNoaaData().getCoolLoad_t()
						.get(date);
			}

		}
	}


}
